

#ifndef __MATH_3D__
#define __MATH_3D__

#include    <math.h>

#include    "Vector3D.h"


Vector3D    closestPointToSegment ( const Vector3D& p, const Vector3D& a, const Vector3D& b );
int			computeNearPointMask  ( const Vector3D& n );

inline float area ( const Vector3D& a, const Vector3D& b, const Vector3D& c )
{
    Vector3D v1 = b - a;
    Vector3D v2 = c - a;

    return ((v1.y*v2.z + v1.z*v2.x + v1.x*v2.y) - (v1.y*v2.x + v1.x*v2.z + v1.z*v2.y));
}

inline float intersectSphereByRay ( const Vector3D& center, float radius, const Vector3D& org, const Vector3D& dir )
{
    Vector3D q = center - org;
    float    c = q & q;             // squared length
    float    v = q & dir;
    float    d = radius * radius - (c * c - v* v);

    if ( d < 0.0f )
       return -1.0f;

    return (float)(v - sqrt ( d ));
}

inline Vector3D projectTo3D(int x,int y,Vector3D viewDir,Vector3D upDir,float fov,int height)
{
	Vector3D rightPos  ;
	rightPos.x=  upDir.x*viewDir.y;
	rightPos.y =  upDir.y*viewDir.z;
	rightPos.z =  upDir.z*viewDir.x;
	float distance = height/tan(fov);

	return ((viewDir*distance) - (upDir*y +rightPos*x));
}




#endif
